import { defineStore } from "pinia";
import {
  deletePageById, editPageData,
  getDepartmentList, getMenuList,
  getRoleList, newPageData,
  postPageListData,
  postUsersListDataAPI
} from "@/services/modules/main/system/system";
import type { ISystemState } from "@/stores/modules/main/system/type";

const useSystemStore = defineStore("system", {
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0,
    roleList: [],
    departmentList: [],
    menuList: [],
    pageList: [],
    pageTotalCount: 0
  }),
  actions: {
    async postUsersListAction(queryInfo) {
      const usersListResult = await postUsersListDataAPI(queryInfo);
      this.usersList = usersListResult?.data.list;
      this.usersTotalCount = usersListResult?.data.totalCount;
    },
    async postRoleListAction(pageInfo?: { offset: number, size: number }) {
      const roleListResult = await getRoleList(pageInfo);
      this.roleList = roleListResult?.data.list;
    },
    async postDepartmentListAction(pageInfo?: { offset: number, size: number }) {
      const departmentListResult = await getDepartmentList(pageInfo);
      this.departmentList = departmentListResult?.data.list;
    },
    async postMenuListAction() {
      const menuListResult = await getMenuList();
      this.menuList = menuListResult?.data.list;
    },
    /**
     * 封装通用页面的增删改查
     */
    async postPageListAction(pageName: string, queryInfo: any) {
      const pageListResult = await postPageListData(pageName, queryInfo);
      const { totalCount, list } = pageListResult?.data;
      this.pageList = list;
      this.pageTotalCount = totalCount;
    },
    async deletePageByIdAction(pageName: string, id: number) {
      const deleteResult =  await deletePageById(pageName, id);
      this.postPageListAction(pageName, { offset: 0, size: 10 });
    },
    async newPageDataAction(pageName: string, pageInfo: any) {
      const newResult = await newPageData(pageName, pageInfo);
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    },
    async editPageDataAction(pageName: string, id: number, pageInfo: any) {
      const editResult = await editPageData(pageName, id, pageInfo);
      this.postPageListAction(pageName, { offset: 0, size: 10 })
    },
  }
});

export default useSystemStore;
